package top.lshaci.learning.hutool;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

/**
 * LocalDateTimeInstant
 *
 * @author liangxu
 */
public class LocalDateTimeInstant {

    public static void main(String[] args) {
        localDateTime2Instant();
        instant2LocalDateTime();

        parseLocalDateTime();
        parseInstant();

        formatLocalDateTime();
        formatInstant();
    }

    private static void formatInstant() {
        System.err.println("====== formatInstant =====");
        DateTimeFormatter formatter = DatePattern.NORM_DATETIME_FORMATTER.withZone(ZoneId.systemDefault());
        String instantStr = formatter.format(Instant.now());
        System.err.println("【DateTimeFormatter#withZone】【" + DatePattern.NORM_DATETIME_PATTERN + "】" + instantStr);
        String instantAtZone = DatePattern.NORM_DATETIME_FORMATTER.format(Instant.now().atZone(ZoneId.systemDefault()));
        System.err.println("【DateTimeFormatter Instant#atZone】【" + DatePattern.NORM_DATETIME_PATTERN + "】" + instantAtZone);
        System.err.println();
    }

    private static void formatLocalDateTime() {
        System.err.println("====== formatLocalDateTime =====");
        String localDateTimeStr = LocalDateTimeUtil.formatNormal(LocalDateTime.now());
        System.err.println("【formatNormal】" + localDateTimeStr);
        System.err.println();
    }

    private static void parseInstant() {
        System.err.println("====== parseInstant =====");
        System.err.println("将字符串转为 LocalDateTime, 然后通过 LocalDateTime 转为 Instant");

        Instant instant = DateUtil.parse("2023-06-09 15:04:50").toInstant();
        System.err.println("【parseToDate toInstant】" + instant);
        System.err.println();
    }

    private static void parseLocalDateTime() {
        System.err.println("====== parseLocalDateTime =====");
        LocalDateTime localDateTimeNorm = LocalDateTimeUtil.parse("2023-06-09 15:04:50", DatePattern.NORM_DATETIME_PATTERN);
        System.err.println("【" + DatePattern.NORM_DATETIME_PATTERN + "】localDateTime: " + localDateTimeNorm);

        LocalDateTime localDateTimeUtc = LocalDateTimeUtil.parse("2023-06-09T15:04:50", DatePattern.UTC_SIMPLE_PATTERN);
        System.err.println("【" + DatePattern.UTC_SIMPLE_PATTERN + "】localDateTime: " + localDateTimeUtc);

        LocalDateTime localDateTimeDefault = LocalDateTimeUtil.parse("2023-06-09T15:04:50");
        System.err.println("【DefaultPattern】localDateTime: " + localDateTimeDefault);

        TemporalAccessor temporalAccessor = DatePattern.NORM_DATETIME_FORMATTER.parse("2023-06-09 15:04:50");
        LocalDateTime localDateTimeFrom = LocalDateTime.from(temporalAccessor);
        System.err.println("【LocalDateTime.from】localDateTime: " + localDateTimeFrom);
        System.err.println();
    }

    private static void instant2LocalDateTime() {
        System.err.println("====== instant2LocalDateTime =====");
        Instant instant = Instant.now();
        LocalDateTime localDateTime = LocalDateTimeUtil.of(instant);
        System.err.println("instant: " + instant);
        System.err.println("localDateTime: " + localDateTime);
        System.err.println();
    }

    private static void localDateTime2Instant() {
        System.err.println("====== instant2LocalDateTime =====");
        LocalDateTime localDateTime = LocalDateTime.now();
        Instant instantAt = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
        Instant instantFrom = Instant.from(zonedDateTime);
        System.err.println("localDateTime: " + localDateTime);
        System.err.println("【LocalDateTime.atZone.toInstant】instant: " + instantAt);
        System.err.println("【Instant.from(ZonedDateTime.of)】instant: " + instantFrom);
        System.err.println();
    }
}
